//
//  WAAddressController.m
//  WalkiOS
//
//  Created by luqiuan on 2019/10/17.
//  Copyright © 2019 Chenmuzhou. All rights reserved.
//

#import "WAAddressController.h"
#import "WAAddressCell.h"
#import "WAAddNewAddressController.h"
#import "WAAddressModel.h"

@interface WAAddressController ()<DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (assign, nonatomic) NSInteger pageNum;
@property (weak, nonatomic) IBOutlet UIButton *addBtn;
@property (strong, nonatomic) NSMutableArray *dataSouces;
@end

@implementation WAAddressController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"WAAddressCell" bundle:nil] forCellReuseIdentifier:@"WAAddressCell"];
    
    @weakify(self)
    self.tableView.mj_header = [MJRefreshLoadHeader headerWithRefreshingBlock:^{
        @strongify(self)
        self.pageNum = 1;
        [self requestData];
    }];
    self.tableView.mj_footer = [MJRefreshMoreFooter footerWithRefreshingBlock:^{
        @strongify(self)
        self.pageNum ++;
        [self requestData];
    }];
    self.tableView.tableFooterView = [UIView new];
    
    self.addBtn.layer.borderWidth = 1;
    self.addBtn.layer.borderColor = [UIColor whiteColor].CGColor;
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.tableView.mj_header beginRefreshing];
}

- (void)requestData{
    [WNBusinessRequest getAddressListWithPageNum:1 Succ:^(id  _Nonnull responseObject) {
        NSDictionary *dict = (NSDictionary *)responseObject;
        NSInteger total = [dict[@"total"] integerValue];
        NSArray *memberList = [WAAddressModel mj_objectArrayWithKeyValuesArray:dict[@"data"]];
        if (self.pageNum == 1) {
            self.dataSouces = [NSMutableArray new];
        }
        for (WAAddressModel * model in memberList) {
            [self.dataSouces addObject:model];
        }
        if(total <= self.dataSouces.count){
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }else{
            [self.tableView.mj_footer endRefreshing];
        }
        [self.tableView.mj_header endRefreshing];
        if (self.dataSouces.count == 0) {
            self.noDataStr = @"无数据";
        }else{
            [self.tableView reloadData];
        }
        [self.tableView reloadData];
        [self.tableView reloadEmptyDataSet];
    } fail:^(NSString * _Nonnull code, NSString * _Nonnull mes) {
        self.noDataStr = mes;
        [self.tableView reloadEmptyDataSet];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        [WNTool show:mes];
        
    }];
}

#pragma mark - UITableView Delegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WAAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WAAddressCell"];
    WAAddressModel *model = self.dataSouces[indexPath.row];
    cell.model = model;
    [cell setDeleteBlock:^(WAAddressModel * _Nonnull model1) {
        [self deleteAction:model1];
    }];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSouces.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.selectBlcok) {
        self.selectBlcok(self.dataSouces[indexPath.row]);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - DZNEmptyDataSetSource
- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view{
    [self.tableView.mj_header beginRefreshing];
}

- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView{
    return [WNTool requestErrorWithSting:self.noDataStr];
}

- (IBAction)backAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)addNewAction:(id)sender {
    WAAddNewAddressController *VC = [WAAddNewAddressController new];
    [self.navigationController pushViewController:VC animated:YES];
}

- (void)deleteAction:(WAAddressModel *)model{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否删除当前地址" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    
    }];
    UIAlertAction *ensure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self deleteRequest:model];
    }];
    [alertVC addAction:cancle];
    [alertVC addAction:ensure];
    [self presentViewController:alertVC animated:YES completion:nil];
}

- (void)deleteRequest:(WAAddressModel *)model{
    [WNTool showLoadingMessage:@"正在删除"];
    [WNBusinessRequest deleteAddressWithID:[@(model.ID) stringValue] Succ:^(id  _Nonnull responseObject) {
        [WNTool hideLoading];
        [WNTool show:@"删除成功"];
        [self.tableView.mj_header beginRefreshing];
        if(self.seletedID == model.ID && self.seletedID > 0){
            if (self.selectBlcok) {
                self.selectBlcok(nil);
            }
        }
    } fail:^(NSString * _Nonnull code, NSString * _Nonnull mes) {
        [WNTool show:mes];
    }];
}
@end
